//$URL: http://dasding.googlecode.com/svn/branches/testzeugs/api/src/main/java/de/piratech/dasding/api/rest/ActionIdResource.java $
//$Id: ActionIdResource.java 90 2012-11-20 18:42:49Z amuthmann@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.api.rest;

import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.security.annotations.RequireUserRole;
import java.util.Date;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Resource for all calls for an specific action
 *
 * @author deveth0
 */
@Path("/actions/id/{id}")
@Component
public class ActionIdResource extends AbstractResource {

  private static final Logger LOG = LoggerFactory.getLogger(ActionIdResource.class);

  /**
   * Returns a single action by id
   *
   * http://localhost:8080/dasding.api/api/actions/id/1
   *
   * GET
   *
   * @param id
   * @return
   */
  @GET
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Action getAction(
          @PathParam("id") String id) {
    LOG.debug("getEvent({})", id);
    try {
      return getActionData().getAction(id);
    } catch (DatabaseException dbe) {
      throw getBadRequestException(dbe);
    }
  }

  /**
   * Updates the Action with the given id
   *
   * http://localhost:8080/dasding.api/api/actions/id/1
   *
   * PUT
   *
   * @param pId
   * @param pName
   * @param pDescription
   * @param pTime
   *
   * @return
   */
  @PUT
  public Response updateAction(
          @PathParam("id") String pId,
          @QueryParam("name") String pName,
          @QueryParam("description") String pDescription,
          @QueryParam("time") Long pTime) {
    LOG.debug("updateAction(id={}, name={}, description={}, time={})", pId, pName, pDescription, pTime);
    try {
      Action action = getActionData().getAction(pId);
      if (action == null) {
        throw new IllegalArgumentException("action does not exist");
      }
      if (StringUtils.isNotBlank(pName)) {
        action.setName(pName);
      }
      if (StringUtils.isNotBlank(pDescription)) {
        action.setDescription(pDescription);
      }
      if (pTime != null) {
        action.setTime(new Date(pTime));
      }
      if (!action.isValid()) {
        throw new IllegalArgumentException("action not valid");
      }
      getActionData().updateAction(action);
      return getNoContentResponse();
    } catch (DatabaseException dbe) {
      return getBadRequestResponse(dbe);
    }
  }

  /**
   * Sets the given action to status "Canceled"
   *
   * http://localhost:8080/dasding.api/api/action/id/1
   *
   * DELETE
   *
   * @param id
   */
  @DELETE
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  @RequireUserRole(roles = User.USER_ROLE.ADMIN)
  public void deleteAction(@PathParam("id") String id) {
    LOG.debug("deleteAction({})", id);
    try {
      //TODO: set action status to canceled
      getActionData().deleteAction(getActionData().getAction(id));
    } catch (DatabaseException dbe) {
      throw getBadRequestException(dbe);
    }
  }

  @Override
  protected Logger getLogger() {
    return LOG;
  }
}
